package dao;

import utils.connect.ConnectSqlDaoImpl;
import utils.string.sql.T_Equipment;
import utils.string.sql.T_Fix;
import utils.string.sql.T_User;
import vo.TableFix;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

public class TableFixDaoImpl implements TableFixDao {
    ConnectSqlDaoImpl connectSqlDao = ConnectSqlDaoImpl.getInStance();
    @Override
    public HashMap selectAll() {
        HashMap tableFixs = new HashMap();
        try {
            connectSqlDao.initConnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Connection connection = connectSqlDao.getConnection();
        String sql = "select "+
                T_Fix.FIX_ID+","+
                T_Fix.FIX_NUM+","+
                T_Fix.FIX_REASON+","+
                T_Fix.FIX_DATE+","+
                T_User.USER_NAME +","+
                T_Equipment.EQUIP_NAME +","+
                T_Equipment.TABLE_NAME+"."+T_Equipment.EQUIP_ID+","+
                T_Fix.TABLE_NAME+"."+T_Fix.USER_ID+" from " +
                T_Fix.TABLE_NAME+"," +
                T_Equipment.TABLE_NAME+","+
                T_User.TABLE_NAME+" where "+
                T_Fix.TABLE_NAME+"."+T_Fix.USER_ID+" = "+T_User.TABLE_NAME+"."+T_User.USER_ID+" AND "+
                T_Fix.TABLE_NAME+"."+T_Fix.EQUIP_ID+" = "+T_Equipment.TABLE_NAME+"."+T_Equipment.EQUIP_ID;
        try {
            PreparedStatement preparedStatement =connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            System.out.println("sql");
            while(resultSet.next()){
                TableFix tableFix = new TableFix();
                tableFix.setEquip_id(resultSet.getInt(T_Fix.EQUIP_ID));
                tableFix.setEquip_name(resultSet.getString(T_Equipment.EQUIP_NAME));
                tableFix.setFix_id(resultSet.getInt(T_Fix.FIX_ID));
                tableFix.setUser_id(resultSet.getInt(T_User.USER_ID));
                tableFix.setFix_num(resultSet.getInt(T_Fix.FIX_NUM));
                tableFix.setFix_reason(resultSet.getString(T_Fix.FIX_REASON));
                tableFix.setUser_name(resultSet.getString(T_User.USER_NAME));
                tableFix.setFix_date(resultSet.getDate(T_Fix.FIX_DATE));
                tableFixs.put(tableFix.getFix_id(),tableFix);
            }
            return tableFixs;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                connectSqlDao.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

       return tableFixs;
    }

    @Override
    public HashMap selectByUserId(int id) {
        HashMap tableFixs = new HashMap();
        try {
            connectSqlDao.initConnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Connection connection = connectSqlDao.getConnection();//先生成，要不就是null对象
        String sql = "select "+
                T_Fix.FIX_ID+","+
                T_Fix.FIX_NUM+","+
                T_Fix.FIX_REASON+","+
                T_Fix.FIX_DATE+","+
                T_User.USER_NAME +","+
                T_Equipment.EQUIP_NAME +","+
                T_Equipment.TABLE_NAME+"."+T_Equipment.EQUIP_ID+","+
                T_Fix.TABLE_NAME+"."+T_Fix.USER_ID+" from " +
                T_Fix.TABLE_NAME+"," +
                T_Equipment.TABLE_NAME+","+
                T_User.TABLE_NAME+" where "+
                T_Fix.TABLE_NAME+"."+T_Fix.USER_ID+" = "+T_User.TABLE_NAME+"."+T_User.USER_ID+" AND "+
                T_Fix.TABLE_NAME+"."+T_Fix.EQUIP_ID+" = "+T_Equipment.TABLE_NAME+"."+T_Equipment.EQUIP_ID+" AND " +
                T_Fix.TABLE_NAME+"."+T_Fix.USER_ID + " = ?";
        try {
            PreparedStatement preparedStatement =connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            ResultSet resultSet = preparedStatement.executeQuery();
            System.out.println("sql");
            while(resultSet.next()){
                TableFix tableFix = new TableFix();
                tableFix.setEquip_id(resultSet.getInt(T_Fix.EQUIP_ID));
                tableFix.setEquip_name(resultSet.getString(T_Equipment.EQUIP_NAME));
                tableFix.setFix_id(resultSet.getInt(T_Fix.FIX_ID));
                tableFix.setUser_id(resultSet.getInt(T_User.USER_ID));
                tableFix.setFix_num(resultSet.getInt(T_Fix.FIX_NUM));
                tableFix.setFix_reason(resultSet.getString(T_Fix.FIX_REASON));
                tableFix.setUser_name(resultSet.getString(T_User.USER_NAME));
                tableFix.setFix_date(resultSet.getDate(T_Fix.FIX_DATE));
                tableFixs.put(tableFix.getFix_id(),tableFix);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                connectSqlDao.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return tableFixs;
    }
}
